#!/bin/bash
#
# This script is used for Darwin builds of the "release-dist" and "sdk-dist" targets. 
# The dist root for PackageMaker is at $AAFSDK/Dist/root
# Additional resources are assembled and the package is built.
# This script is, for the most part, a hack. Things to make it better would include
# using separate template files, or a scriptable plist editor, to make plists. 
#
AAFSDK="$1" 
DIST_FILE_NAME="$2"
AAFSDKBUILD_H="$3"
RELEASE_TYPE="$4"

AAF_MAJOR_VERSION=`grep "define AAF_MAJOR_VERSION" ${AAFSDKBUILD_H} | awk '{print $3}'`;
AAF_MINOR_VERSION=`grep "define AAF_MINOR_VERSION" ${AAFSDKBUILD_H} | awk '{print $3}'`;
AAF_MAINT_RELEASE=`grep "define AAF_MAINT_RELEASE" ${AAFSDKBUILD_H} | awk '{print $3}'`;
AAF_RELEASE_STAGE=`grep "define AAF_RELEASE_STAGE" ${AAFSDKBUILD_H} | awk '{print $3}'`;
AAF_PATCH_LEVEL=`grep "define AAF_PATCH_LEVEL"  ${AAFSDKBUILD_H} | awk '{print $3}'`;
BUNDLE_NAME="AAF $RELEASE_TYPE"
GET_INFO_STRING="$BUNDLE_NAME $AAF_MAJOR_VERSION.$AAF_MINOR_VERSION.$AAF_MAINT_RELEASE($AAF_PATCH_LEVEL)"

cd "$AAFSDK"
filename="$DIST_FILE_NAME.pkg"
mkdir -p Dist/res
cp ../../LEGAL/AAFSDKPSL.TXT Dist/res/License.txt
cat >Dist/res/Info.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleGetInfoString</key>
    <string>$GET_INFO_STRING</string>
    <key>CFBundleIdentifier</key>
    <string>org.aafassociation.aafsdk</string>
    <key>CFBundleName</key>
    <string>$BUNDLE_NAME</string>
    <key>CFBundleShortVersionString</key>
    <string>$AAF_MAJOR_VERSION.$AAF_MINOR_VERSION.$AAF_MAINT_RELEASE</string>
    <key>IFMajorVersion</key>
    <integer>$AAF_MAJOR_VERSION</integer>
    <key>IFMinorVersion</key>
    <integer>$AAF_MINOR_VERSION</integer>
    <key>IFPkgFlagAllowBackRev</key>
    <false/>
    <key>IFPkgFlagAuthorizationAction</key>
    <string>RootAuthorization</string>
    <key>IFPkgFlagDefaultLocation</key>
    <string>/usr/local</string>
    <key>IFPkgFlagFollowLinks</key>
    <true/>
    <key>IFPkgFlagInstallFat</key>
    <false/>
    <key>IFPkgFlagIsRequired</key>
    <false/>
    <key>IFPkgFlagOverwritePermissions</key>
    <false/>
    <key>IFPkgFlagRelocatable</key>
    <true/>
    <key>IFPkgFlagRestartAction</key>
    <string>NoRestart</string>
    <key>IFPkgFlagRootVolumeOnly</key>
    <false/>
    <key>IFPkgFlagUpdateInstalledLanguages</key>
    <false/>
    <key>IFPkgFlagUseUserMask</key>
    <false/>
    <key>IFPkgFormatVersion</key>
    <real>0.10000000149011612</real>
</dict>
</plist>
EOF

cat >Dist/res/Description.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>IFPkgDescriptionTitle</key>
    <string>$BUNDLE_NAME</string>
    <key>IFPkgDescriptionVersion</key>
    <string>$AAF_MAJOR_VERSION.$AAF_MINOR_VERSION.$AAF_MAINT_RELEASE($AAF_PATCH_LEVEL)</string>
</dict>
</plist>
EOF

/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker \
	-build \
	-p $PWD/$filename \
	-f $PWD/Dist/root \
	-i $PWD/Dist/res/Info.plist \
	-d $PWD/Dist/res/Description.plist \
	-r $PWD/Dist/res

pushd Dist
# create paths for packaging into tar file by stripping "Dist" from names
tar_file=`echo $DIST_FILE_NAME.tar.gz | sed 's/^Dist\///'`
dir_to_packup=`echo $filename | sed 's/^Dist\///'`

tar czvf $tar_file $dir_to_packup
popd

rm -rf Dist/res
